package study.android.chajajo;

import android.app.Activity;
import android.content.ComponentName;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

public class MainActivity extends Activity {

	ComponentName worker = null;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		Button registBtn = (Button) findViewById(R.id.registBtn);
		Button unRegistBtn = (Button) findViewById(R.id.unRegistBtn);
		final EditText keywordView = (EditText) findViewById(R.id.keyword);
		final EditText secView = (EditText) findViewById(R.id.sec);
		registBtn.setOnClickListener(new View.OnClickListener() {
			public void onClick(View v) {
				String keyword = keywordView.getText().toString();
				String sec = secView.getText().toString();
				Intent intent = new Intent(MainActivity.this, WorkerService.class);
				intent.putExtra("SEC", sec);
				intent.putExtra("KEYWORD", keyword);
				worker = startService(intent);
			};
		});

		unRegistBtn.setOnClickListener(new View.OnClickListener() {
			public void onClick(View v) {
				String msg;
				if (worker == null) {
					msg = "No running worker!";
				} else {
					msg = "Stop worker!";
					//stopService(new Intent(MainActivity.this, worker.getClass()));
					stopService(new Intent(MainActivity.this, WorkerService.class));
					worker = null;
				}

				Toast.makeText(MainActivity.this, msg, Toast.LENGTH_SHORT).show();
			};
		});

	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.main, menu);
		return true;
	}

	@Override
	public void onStart() {
		super.onStart();

	}

}
